#include<bits/stdc++.h>
using namespace std;

const int N = 100010;

int n, m;
int h[N], e[N], ne[N], idx; 
bool st[N];
queue<int> q;

void add(int a, int b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

int bfs() {
    q.push(1);
    st[1] = true;
    int ans = 0;
    while (!q.empty()) {
        int len = q.size();
        for (int k = 0; k < len; k++) {
            int t = q.front(); q.pop();
            if (t == n) return ans;
            for (int i = h[t]; i != -1; i = ne[i]) {
                int j = e[i];
                if (!st[j]) {
                    st[j] = true;
                    q.push(j);
                }
            }
        }
        ans++;
    }
    return -1;
}

int main() {
    idx = 0;
    memset(h, -1, sizeof h);
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int a, b;
        cin >> a >> b;
        add(a, b);
    }
   
    cout << bfs() << endl;
    return 0;
}